package org.csu.mypetstore.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/catalog")
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    //第一次进入主页面
    @GetMapping("/firstView")
    public String firstView(HttpSession session){
        Account account = new Account();
        Cart cart = new Cart();
        Order order = new Order();
        account = null;
        cart = null;
        order = null;

        session.setAttribute("account",account);
        session.setAttribute("cart",cart);
        session.setAttribute("order",order);
        session.setAttribute("isAdministrator", false);

        return "catalog/main";
    }

    //返回主页面
    @GetMapping("/view")
    public String view(){
        return "catalog/main";
    }

    //跳转到商品大类（Product）目录
    @GetMapping("/viewCategory")
    public String viewCategory(String categoryId, Model model){
        if(categoryId != null){
            Category category = catalogService.getCategory(categoryId);
            List<Product> productList = catalogService.getProductListByCategory(categoryId);
            model.addAttribute("category",category);
            model.addAttribute("productList",productList);
            return "catalog/Category";
        }
        return "catalog/main";
    }

    //跳转到商品具体类（Item）目录
    @GetMapping("/viewProduct")
    public String viewProduct(String productId, Model model){
        if(productId != null){
            Product product = catalogService.getProduct(productId);
            List<Item> itemList = catalogService.getItemListByProduct(productId);
            model.addAttribute("product",product);
            model.addAttribute("itemList",itemList);
            return "catalog/Product";
        }
        return "catalog/main";
    }

    //跳转到具体类
    @GetMapping("/viewItem")
    public String viewItem(String itemId,String productId, Model model){
        if(itemId != null){
            Item item = catalogService.getItem(itemId);
            Product product = catalogService.getProduct(productId);
            model.addAttribute("item",item);
            model.addAttribute("product",product);
            return "catalog/Item";
        }
        return "catalog/main";
    }

    //跳转到管理员主页
    @GetMapping("/viewAdminmain")
    public String viewAdminmain(){
        return "catalog/Adminmain";
    }

    //悬浮窗显示
    @GetMapping("/categoryShowJs")
    public void categoryShowJs(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String categoryId = request.getParameter("categoryId");
        System.out.println(categoryId+"             ddddddddd");
        List<Product> productList = catalogService.getProductListByCategory(categoryId);
        String resp = "Product ID      Name\n";
        int i = 0;
        while(i < productList.size()){
            Product product = productList.get(i);
            resp += product.getProductId() + "      " + product.getName() + "\n";
            i++;
        }

        response.setContentType("text/xml");
        PrintWriter out = response.getWriter();
        out.write(resp);
    }

    //搜索补全框显示
    @GetMapping("/fill")
    public void fill(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String keyword = request.getParameter("reg_city");
        List<Product> productList = catalogService.searchProductList(keyword);

        List<String> choice = new ArrayList<>();

        for (Product o:productList
        ) {
            choice.add(o.getName());
        }
        ObjectMapper mapper = new ObjectMapper(); // 提供java-json相互转换功能的类

        String json = mapper.writeValueAsString(choice); // 将list中的对象转换为Json字符串
        response.setContentType("text/html; charset=utf-8");
        response.getWriter().write(json);
    }

    @PostMapping("/searchProduct")
    public String searchProduct(HttpServletRequest req, Model model){
        String keyword=req.getParameter("keyword");
        List<Product> productList=catalogService.searchProductList(keyword);

        model.addAttribute("productList",productList);

        return "/catalog/SearchProducts";
    }


}
